﻿using AuthCenter_HuangJingtao.Domain;
using AuthCenter_HuangJingtao.ErrorCode;
using AuthCenter_HuangJingtao.Read.Api.Application.Command;
using AuthCenter_HuangJingtao.Read.Api.Dto;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace AuthCenter_HuangJingtao.Read.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class RolesAllController : ControllerBase
    {
        IMediator mediator;

        public RolesAllController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        [HttpGet]
        public async Task<ApiResult<List<Roles>>> ShowRoles()
        {
            try
            {
                RoleShowCommand RoleShowCommand = new RoleShowCommand();
                return await mediator.Send(RoleShowCommand);
            }
            catch (Exception)
            {
                throw;
            }
        }

        [HttpPost]
        public async Task<ApiResult<int>> UpdateRole(Roles Roles)
        {
            try
            {
                UpdateRoleCommand UpdateRoleCommand = new UpdateRoleCommand()
                {
                    Roles = Roles
                };
                return await mediator.Send(UpdateRoleCommand);
            }
            catch (Exception)
            {
                throw;
            }
        }

        [HttpGet]
        public async Task<ApiResult<Roles>> FirstRoles(int id)
        {
            try
            {
                FirstRoleCommand FirstRoleCommand = new FirstRoleCommand()
                {
                    id = id
                };
                return await mediator.Send(FirstRoleCommand);
            }
            catch (Exception)
            {
                throw;
            }
        }

        [HttpDelete]
        public async Task<ApiResult<int>> DelRole(int id)
        {
            try
            {
                DelRoleCommand DelRoleCommand = new DelRoleCommand()
                {
                    id = id
                };
                return await mediator.Send(DelRoleCommand);
            }
            catch (Exception)
            {
                throw;
            }
        }

        [HttpGet]
        public async Task<ApiResult<List<App>>> QueryName(string Name)
        {
            try
            {
                RoleQueryNameCommand RoleQueryNameCommand = new RoleQueryNameCommand()
                {
                    Name = Name
                };
                return await mediator.Send(RoleQueryNameCommand);
            }
            catch (Exception)
            {
                throw;
            }
        }

        [HttpPost]
        public async Task<ApiResult<int>> AddRole(Roles Roles)
        {
            try
            {
                AddRoleCommand AddRoleCommand = new AddRoleCommand()
                {
                    Roles = Roles
                };
                return await mediator.Send(AddRoleCommand);
            }
            catch (Exception)
            {
                throw;
            }
        }

        [HttpGet]
        public async Task<ApiResult<List<ApiTree>>> SeAppShow(int Group_Id)
        {
            try
            {
                SeAppCommand SeAppCommand = new SeAppCommand()
                {
                    Group_Id = Group_Id
                };
                return await mediator.Send(SeAppCommand);
            }
            catch (Exception)
            {
                throw;
            }
        }

        [HttpGet]
        public async Task<ApiResult<int>> UpdateApp(int App_Id,int Role_Id)
        {
            try
            {
                UpdateAppPermissionCommand UpdateAppPermissionCommand = new UpdateAppPermissionCommand()
                {
                    App_Id = App_Id,
                    Role_Id=Role_Id
                };
                return await mediator.Send(UpdateAppPermissionCommand);
            }
            catch (Exception)
            {
                throw;
            }
        }

        [HttpGet]
        public async Task<ApiResult<int>> PruductAll(string App_Id, int Group_Id, int Role_Id)
        {
            try
            {
                AddGroupShipCommand AddGroupShipCommand = new AddGroupShipCommand()
                {
                    Group_Id = Group_Id,
                    App_Id = App_Id,
                    Role_Id = Role_Id
                };
                return await mediator.Send(AddGroupShipCommand);
            }
            catch (Exception)
            {
                throw;
            }
        }
        [HttpGet]
        public async Task<ApiResult<string>> FistRoleContain(int Group_Id,int Role_Id)
        {
            try
            {
                FistRoleContainCommand FistRoleContainCommand = new FistRoleContainCommand() 
                {
                    GroupId = Group_Id,
                    RoleId = Role_Id
                };
                return await mediator.Send(FistRoleContainCommand);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
